/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.entities;

import com.helpdesk.controllers.util.Cookier;
import com.helpdesk.controllers.util.JsfUtil;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.Generated;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author JK
 */
@Entity
@Table(name = "Accounts")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Accounts.findAll", query = "SELECT a FROM Accounts a"),
    @NamedQuery(name = "Accounts.findById", query = "SELECT a FROM Accounts a WHERE a.id = :id"),
    @NamedQuery(name = "Accounts.findByUsername", query = "SELECT a FROM Accounts a WHERE a.username = :username"),
    @NamedQuery(name = "Accounts.findByPassword", query = "SELECT a FROM Accounts a WHERE a.password = :password"),
    @NamedQuery(name = "Accounts.findByFirstname", query = "SELECT a FROM Accounts a WHERE a.firstname = :firstname"),
    @NamedQuery(name = "Accounts.findByLastname", query = "SELECT a FROM Accounts a WHERE a.lastname = :lastname"),
    @NamedQuery(name = "Accounts.findByBirthday", query = "SELECT a FROM Accounts a WHERE a.birthday = :birthday"),
    @NamedQuery(name = "Accounts.findByEmail", query = "SELECT a FROM Accounts a WHERE a.email = :email"),
    @NamedQuery(name = "Accounts.findByDateCreated", query = "SELECT a FROM Accounts a WHERE a.dateCreated = :dateCreated"),
    @NamedQuery(name = "Accounts.findByLastUpdate", query = "SELECT a FROM Accounts a WHERE a.lastUpdate = :lastUpdate"),
    @NamedQuery(name = "Accounts.findByLastVisit", query = "SELECT a FROM Accounts a WHERE a.lastVisit = :lastVisit"),
    @NamedQuery(name = "Accounts.findByRoles", query = "SELECT a FROM Accounts a WHERE a.roles = :roles"),
    @NamedQuery(name = "Accounts.findByStatus", query = "SELECT a FROM Accounts a WHERE a.status = :status")})
public class Accounts implements Serializable {
    @Id
//    @Basic(optional = false)
//    @NotNull
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Username")
    private String username;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Password")
    private String password;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Firstname")
    private String firstname;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Lastname")
    private String lastname;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Birthday")
    @Temporal(TemporalType.TIMESTAMP)
    private Date birthday;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Email")
    private String email;
    @Column(name = "DateCreated")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreated;
    @Column(name = "LastUpdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdate;
    @Column(name = "LastVisit")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastVisit;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Roles")
    private int roles;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Status")
    private int status;
    @JoinColumn(name = "DepartmentID", referencedColumnName = "ID")
    @ManyToOne
    private Departments departmentID;
    @OneToMany(mappedBy = "assignedTo", orphanRemoval = true)
    private List<Complaints> assignedComplaintsList;
    @OneToMany(mappedBy = "reporter", orphanRemoval = true)
    private List<Complaints> createdComplaintsList;
    @OneToMany(mappedBy = "accountID", orphanRemoval = true)
    private List<Notes> notesList;
    @OneToMany(mappedBy = "accountID", orphanRemoval = true)
    private List<Posts> postsList;
    @OneToMany(mappedBy = "accountID", orphanRemoval = true)
    private List<ComplaintAssignToAccounts> complaintAssignToAccountsList;

    public Accounts() {
    }

    public Accounts(Integer id) {
        this.id = id;
    }

    public Accounts(Integer id, String username, String password, String firstname, String lastname, Date birthday, String email, int roles, int status) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.birthday = birthday;
        this.email = email;
        this.roles = roles;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public Date getLastVisit() {
        return lastVisit;
    }

    public void setLastVisit(Date lastVisit) {
        this.lastVisit = lastVisit;
    }

    public int getRoles() {
        return roles;
    }

    public void setRoles(int roles) {
        this.roles = roles;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Departments getDepartmentID() {
        return departmentID;
    }

    public void setDepartmentID(Departments departmentID) {
        this.departmentID = departmentID;
    }

    @Transient
    public String getHomePageUrl() {
        switch (getRoles()) {
            case HelpDeskEnums.UserRoles.Employee:

                return "employee/home.jsf";
            case HelpDeskEnums.UserRoles.TechMan:
                return "techman/home.jsf";
            case HelpDeskEnums.UserRoles.Admin:
                return "admin/home.jsf";
            default:
                Cookier.remove("_ua");
                HttpSession session = JsfUtil.getSession(true);
                if (session != null) {

                    session.invalidate();
                }
                return "home.jsf";

        }
    }

    @XmlTransient
    public List<Complaints> getAssignedComplaintsList() {
        return assignedComplaintsList;
    }

    public void setAssignedComplaintsList(List<Complaints> complaintsList) {
        this.assignedComplaintsList = complaintsList;
    }

    @XmlTransient
    public List<Complaints> getCreatedComplaintsList() {
        return createdComplaintsList;
    }

    public void setCreatedComplaintsList(List<Complaints> complaintsList1) {
        this.createdComplaintsList = complaintsList1;
    }

    @XmlTransient
    public List<Notes> getNotesList() {
        return notesList;
    }

    public void setNotesList(List<Notes> notesList) {
        this.notesList = notesList;
    }

    @XmlTransient
    public List<Posts> getPostsList() {
        return postsList;
    }

    public void setPostsList(List<Posts> postsList) {
        this.postsList = postsList;
    }

    @XmlTransient
    public List<ComplaintAssignToAccounts> getComplaintAssignToAccountsList() {
        return complaintAssignToAccountsList;
    }

    public void setComplaintAssignToAccountsList(List<ComplaintAssignToAccounts> complaintAssignToAccountsList) {
        this.complaintAssignToAccountsList = complaintAssignToAccountsList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Accounts)) {
            return false;
        }
        Accounts other = (Accounts) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return firstname + " " + lastname;
    }
}
